package com.wfh.aihelper.ai;

import com.wfh.aihelper.ai.tools.InterviewQuestionTool;
import com.wfh.aihelper.guardrail.SafeInputGuardrail;
import dev.langchain4j.guardrail.InputGuardrail;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Title: AiCOdeHelperServiceFactory
 * @Author wangfenghuan
 * @Package com.wfh.aihelper.ai
 * @Date 2025/7/9 20:19
 * @description: 工厂类
 */
@Configuration
public class AiCodeHelperServiceFactory {

    @Resource
    private ChatModel myQwenChatModel;

    @Resource
    private ContentRetriever contentRetriever;

    @Resource
    private McpToolProvider mcpToolProvider;

    @Resource
    private StreamingChatModel qwenStreamingChatModel;

    @Bean
    public AiCodeHelperService createAiCodeHelperService() {
        // 每个用户最多保存10条的消息，会话记忆
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        return AiServices.builder(AiCodeHelperService.class)
                .chatModel(myQwenChatModel)
                // 会话记忆
                .chatMemory(chatMemory)
                // 知识库
                .contentRetriever(contentRetriever)
                // 工具调用
                .tools(new InterviewQuestionTool())
                .toolProvider(mcpToolProvider)
                .inputGuardrails(new SafeInputGuardrail())
                .streamingChatModel(qwenStreamingChatModel)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                .build();
    }
}
